
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.PircBot;
import org.jibble.pircbot.User;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Aksel Kvitberg
 */
public class RegExBot extends PircBot {

	HashMap<String, String> userCompliments = new HashMap<String, String>();
	ArrayList<String> compliments = new ArrayList<String>();
	Random r = new Random();

	public RegExBot() {
		readCompliments();

		this.setName("ComplimentBot");
		this.setLogin("complimentBot");
		try {
			this.connect("irc.lessthan3.net");
			this.joinChannel("#zubac2");
		} catch (IOException ex) {
			Logger.getLogger(RegExBot.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IrcException ex) {
			Logger.getLogger(RegExBot.class.getName()).log(Level.SEVERE, null, ex);
		}

	}

	public void onMessage(String channel, String sender, String login, String hostname, String message) {

		Pattern p = Pattern.compile("\\A[cC]ompliment (.+)");
		Matcher m = p.matcher(message);
		if (m.find()) {
			String whom = m.group(1);
			if (whom.equals("me")) {
				sendMessage(channel, sender + " " + getCompliment());
			} else {
				sendMessage(channel, whom + " " + getCompliment());
			}
		}

//		setComplimentCheck(channel, sender, login, hostname, message);
//		getComplimentCheck(channel, sender, login, hostname, message);
//		getComplimentMeCheck(channel, sender, login, hostname, message);
	}

	public void setComplimentCheck(String channel, String sender, String login, String hostname, String message) {
		Pattern p = Pattern.compile("\\A(\\w+) (is|are) (\\w+)");
		Matcher m = p.matcher(message);
		System.out.println(message);

		if (m.find()) {
			String name = m.group(1);
			String isAre = m.group(2);
			String desc = m.group(3);

			if (isInChannel(channel, name)) {
				System.out.println(name + " " + isAre + " " + desc);
				sendMessage(channel, name + " " + isAre + " " + desc);
				userCompliments.put(name, isAre + " " + desc);
			}
		}
	}

	private void getComplimentCheck(String channel, String sender, String login, String hostname, String message) {
		Pattern p = Pattern.compile("\\Acompliment (\\w+)");
		Matcher m = p.matcher(message.toLowerCase());

		if (m.find()) {
			String user = m.group(1);
			if (isInChannel(channel, user)) {
				sendMessage(channel, user + " " + userCompliments.get(user));
			}
		}
	}

	private void getComplimentMeCheck(String channel, String sender, String login, String hostname, String message) {
		Pattern p = Pattern.compile("\\Acompliment me");
		Matcher m = p.matcher(message);

		if (m.find()) {
			sendMessage(channel, sender + " " + userCompliments.get(sender));
		}
	}

	public void onNickChange(String oldNick, String login, String hostname, String newNick) {
		super.onNickChange(oldNick, login, hostname, newNick);
		String compliment = userCompliments.get(oldNick);
		userCompliments.remove(oldNick);
		userCompliments.put(newNick, compliment);
	}

	public static void main(String args[]) {
		new RegExBot();
	}

	private boolean isInChannel(String channel, String nick) {
		for (User user : this.getUsers(channel)) {
			if (user.getNick().equals(nick)) {
				return true;
			}
		}
		return false;
	}

	private void readCompliments() {
		try {
			BufferedReader br = null;
			File f = new File("compliments.txt");
			br = new BufferedReader(new FileReader(f));
			String line = null;
			while ((line = br.readLine()) != null) {
				compliments.add(line);
			}
		} catch (IOException ex) {
			Logger.getLogger(RegExBot.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	private String getCompliment() {
		return compliments.get(r.nextInt(compliments.size()));
	}
}
